// logs.js
const util = require('../../utils/util.js')
const auth = require('../../utils/auth.js')

Page({
  data: {
    logs: [],
    loginStatus: 'unknown',
    userInfo: null,
    testResults: []
  },

  onLoad() {
    this.loadLogs()
    this.testLoginStatus()
  },

  loadLogs() {
    const logs = wx.getStorageSync('logs') || []
    this.setData({
      logs: logs.map(log => new Date(log))
    })
  },

  /**
   * 测试登录状态检查功能
   */
  async testLoginStatus() {
    const results = []
    
    try {
      // 测试1: 检查登录状态
      results.push('开始测试登录状态检查...')
      const loginStatus = await auth.checkLoginStatus()
      results.push(`登录状态检查结果: ${loginStatus.isLoggedIn ? '已登录' : '未登录'}`)
      results.push(`状态信息: ${loginStatus.message}`)
      
      if (loginStatus.isLoggedIn) {
        this.setData({
          loginStatus: 'logged_in',
          userInfo: loginStatus.userInfo
        })
        results.push(`用户信息: ${JSON.stringify(loginStatus.userInfo, null, 2)}`)
      } else {
        this.setData({
          loginStatus: 'not_logged_in',
          userInfo: null
        })
      }
      
      // 测试2: 获取本地token
      const token = auth.getToken()
      results.push(`本地Token: ${token ? token.substring(0, 20) + '...' : '无'}`)
      
      // 测试3: 获取本地用户信息
      const userInfo = auth.getUserInfo()
      results.push(`本地用户信息: ${userInfo ? '存在' : '无'}`)
      
      // 测试4: 验证token有效性
      if (token) {
        results.push(`Token: ${token.substring(0, 20) + '...'}`)
      }
      
    } catch (error) {
      results.push(`测试失败: ${error.message}`)
      console.error('登录状态测试失败:', error)
    }
    
    this.setData({
      testResults: results
    })
  },

  /**
   * 手动登录测试
   */
  async testManualLogin() {
    const results = [...this.data.testResults]
    results.push('开始测试手动登录...')
    
    try {
      const loginResult = await auth.manualLogin()
      
      if (loginResult.success) {
        results.push('手动登录成功')
        results.push(`用户信息: ${JSON.stringify(loginResult.userInfo, null, 2)}`)
        
        this.setData({
          loginStatus: 'logged_in',
          userInfo: loginResult.userInfo
        })
      } else {
        results.push(`手动登录失败: ${loginResult.message}`)
      }
    } catch (error) {
      results.push(`手动登录测试失败: ${error.message}`)
    }
    
    this.setData({
      testResults: results
    })
  },

  /**
   * 自动登录测试
   */
  async testAutoLogin() {
    const results = [...this.data.testResults]
    results.push('开始测试自动登录...')
    
    try {
      const loginResult = await auth.autoLogin()
      
      if (loginResult.success) {
        results.push('自动登录成功')
        results.push(`用户信息: ${JSON.stringify(loginResult.userInfo, null, 2)}`)
        
        this.setData({
          loginStatus: 'logged_in',
          userInfo: loginResult.userInfo
        })
      } else {
        results.push(`自动登录失败: ${loginResult.message}`)
      }
    } catch (error) {
      results.push(`自动登录测试失败: ${error.message}`)
    }
    
    this.setData({
      testResults: results
    })
  },

  /**
   * 退出登录测试
   */
  async testLogout() {
    const results = [...this.data.testResults]
    results.push('开始测试退出登录...')
    
    try {
      const logoutResult = await auth.logout()
      results.push(`退出登录结果: ${logoutResult.message}`)
      
      this.setData({
        loginStatus: 'not_logged_in',
        userInfo: null
      })
    } catch (error) {
      results.push(`退出登录测试失败: ${error.message}`)
    }
    
    this.setData({
      testResults: results
    })
  },

  /**
   * 刷新用户信息测试
   */
  async testRefreshUserInfo() {
    const results = [...this.data.testResults]
    results.push('开始测试刷新用户信息...')
    
    try {
      const refreshResult = await auth.refreshUserInfo()
      
      if (refreshResult.success) {
        results.push('刷新用户信息成功')
        results.push(`最新用户信息: ${JSON.stringify(refreshResult.userInfo, null, 2)}`)
        
        this.setData({
          userInfo: refreshResult.userInfo
        })
      } else {
        results.push(`刷新用户信息失败: ${refreshResult.message}`)
      }
    } catch (error) {
      results.push(`刷新用户信息测试失败: ${error.message}`)
    }
    
    this.setData({
      testResults: results
    })
  },

  /**
   * 清除测试结果
   */
  clearTestResults() {
    this.setData({
      testResults: []
    })
  }
})
